package com.lglin.common.config.security;


import com.lglin.common.config.security.filter.AuthenticationTokenFilter;
import com.lglin.common.config.security.filter.SelfAuthenticationFilter;
import com.lglin.common.config.security.handler.SelfAccessDeniedHandler;
import com.lglin.common.config.security.handler.SelfAuthenticationFailureHandler;
import com.lglin.common.config.security.handler.SelfAuthenticationSuccessHandler;
import com.lglin.common.config.security.point.SelfAuthenticationEntryPoint;
import com.lglin.common.config.security.service.SelfUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true,securedEnabled=true,jsr250Enabled=true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private AuthenticationTokenFilter authenticationTokenFilter;
    @Autowired
    private SelfAuthenticationSuccessHandler authenticationSuccessHandler;
    @Autowired
    private SelfAuthenticationFailureHandler authenticationFailureHandler;
    @Autowired
    private SelfUserDetailsService userDetailsService;
    @Autowired
    private SelfAccessDeniedHandler accessDeniedHandler;
    @Autowired
    private SelfAuthenticationEntryPoint authenticationEntryPoint;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder());
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Bean
    SelfAuthenticationFilter authenticationFilter() throws Exception {
        SelfAuthenticationFilter filter = new SelfAuthenticationFilter();
        filter.setAuthenticationSuccessHandler(authenticationSuccessHandler);
        filter.setAuthenticationFailureHandler(authenticationFailureHandler);
        filter.setUsernameParameter("email");
        filter.setFilterProcessesUrl("/user/login");
        //这句很关键，重用WebSecurityConfigurerAdapter配置的AuthenticationManager，不然要自己组装AuthenticationManager
        filter.setAuthenticationManager(authenticationManagerBean());
        return filter;
    }

//    @Bean
//    public AccessDeniedHandler getAccessDeniedHandler() {
//        return new SelfAccessDeniedHandler();
//    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers(HttpMethod.GET, UnInterceptorUrls.getTypeUrl()).permitAll();
        http.authorizeRequests().antMatchers(HttpMethod.POST, UnInterceptorUrls.postTypeUrl()).permitAll();
        http.authorizeRequests().antMatchers(HttpMethod.GET,AccessInterceptorUrls.getTypeUrl()).hasRole("ADMIN");
        http.formLogin().loginPage("/view/user/login-register.html");
        http.authorizeRequests().anyRequest().authenticated();
        http.httpBasic().authenticationEntryPoint(authenticationEntryPoint);
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        http.addFilterAt(authenticationFilter(), UsernamePasswordAuthenticationFilter.class);
        http.exceptionHandling().accessDeniedHandler(accessDeniedHandler);
        http.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
        http.csrf().disable();
        http.cors();
    }


    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers(UnInterceptorUrls.staticResourceUrl());
    }
}
